/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spielo.crystaldesigner.dao;

import com.spielo.crystaldesigner.property.PropertyHolder;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

/**
 *
 * @author Daniel
 */
public class ExtractArchive 
{
    private PropertyHolder properties = PropertyHolder.getInstance();

       
    

    public void extractArchive(File archive) {
        ZipFile zipFile = null;
        String path = System.getProperty("java.io.tmpdir")  + "CrystalDesigner";
        properties.addProperty("path", path);
        try {
             path = (String) properties.getProperty("path");
            File destDir = new File(path);

            if (!destDir.exists()) {
                destDir.mkdir();
            } else {
                this.deleteDir(destDir);
            }

            zipFile = new ZipFile(archive);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();

            byte[] buffer = new byte[1000];
            int len;
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();

                String entryFileName = entry.getName();

                File dir = buildDirectoryHierarchyFor(entryFileName, destDir);

                if (!dir.exists()) {
                    dir.mkdirs();
                }

                if (!entry.isDirectory()) {
                    File targetFile = new File(destDir, entryFileName);
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
                    BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

                    while ((len = bis.read(buffer)) > 0) {
                        bos.write(buffer, 0, len);
                    }

                    bos.flush();
                    bos.close();
                    bis.close();
                }
            }
        } catch (IOException ex) {
            System.err.println("Exception in extractArchive1: " + ex.getMessage());
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (IOException ex) {
                    System.out.println("Exception in extractArchive2: " + ex.getMessage());
                }
            }
        }

    }

    private File buildDirectoryHierarchyFor(String entryName, File destDir) {
        int lastIndex = entryName.lastIndexOf('/');
        String internalPathToEntry = entryName.substring(0, lastIndex + 1);
        return new File(destDir, internalPathToEntry);
    }

    private boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return dir.delete();
    }
}
